<?php
namespace app\api\controller\phone;

use app\common\controller\Api;
use app\api\controller\phone\Variable;
use app\api\model\wanlshop\Recharge as RechargeModel;
use think\Request;
use think\Log;
class Recharge extends Api
{
    protected $noNeedLogin = '*';
    protected $noNeedRight='*';
    public $orderCallbackUrl = "https://wanlshop.enjoysix.com/api/phone/recharge/callback";
    public function main($url,$data=[]){
        $variable = new Variable();
        return $variable->main($url,$data);
    }
    //商品列表
    public function list(){
        $url = "/api/sideline/agent/getProductList";
        return $this->success('返回成功',  $this->main($url));
    }
    //提交订单
    public function submit_order(){
        $requestId = '888'.time() * 1000;
        $productId = $this->request->post('productId');
        $faceValue = $this->request->post('faceValue');
        $account = $this->request->post('account');
        $data = [
            'requestId'=>$requestId, //客户请求单号，同一单号不可重复提交
            'productId'=>$productId, //下单商品id
            'faceValue'=>$faceValue, //充值账号，户号/手机号
            'account'=>$account, //充值账号，户号/手机号
            'orderCallbackUrl'=>"https://wanlshop.enjoysix.com/api/phone/recharge/callback"
        ];
        $url = "/api/sideline/order/submitOrder";
        return $this->success('返回成功',  $this->main($url,$data));
    }
    //查询订单

    /**
     * @return null
     */
    public function get_order(){
        $requestId = $this->request->post('requestId');
        $data = [
            'requestId'=>$requestId, //客户请求单号
        ];
        $url = "/api/sideline/order/getOrder";
        return $this->success('返回成功',  $this->main($url,$data));
    }

    //订单结果回调
    public function callback(Request $request){
        Log::error($request->post());
        Log::error("321132132131321321");
        $requestId = $this->request->post('requestId');
        $failReason = $this->request->post('failReason');
        $status = $this->request->post('status');
        $successPrice = $this->request->post('successPrice');
        RechargeModel::create([
            'order_no'=>$requestId,
            'fail_reason'=>$failReason,
            'status'=>$status,
            'success_price'=>$successPrice,
        ]);
    }
    //查询余额
    public function get_balance(){
        $url = "/api/sideline/agent/getBalance";
        return $this->success('返回成功',  $this->main($url));
    }
}